﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Con;
using FirebirdSql.Data.FirebirdClient;

namespace SCMainAPP.DAL
{
    abstract class BasicDAL
    {
        static string conPath = null;
        public static FbConnection fbCon = null;
        public static FbTransaction fbTrans = null;

        public BasicDAL()
        {
            
        }

        //Inicializa ConStr
        private static void RetornaConStr()
        {
            IniCon conPathObj = new IniCon();
            conPath = conPathObj.vIniConReader();
        }

        // Abre a conexão para transações
        private static object AbreConnectionFb()
        {
            try
            {
                // Tenta abrir a conexão e se conectar a ela
                RetornaConStr();
                fbCon = new FbConnection(conPath);
                fbCon.Open();
            }
            // Caso ocorra um problema, seta a conexão como null
            catch (Exception ex)
            {
                if (fbCon != null)
                {
                    fbCon = null;
                    fbCon.Dispose();
                }
                throw ex;
            }
            return fbCon;
        }

        // Fecha a conexão
        private static void FechaConnectionFb()
        {
            if (fbCon.State == ConnectionState.Open)
                fbCon.Close();
        }

        //Inicia as transações
        public static void IniciaTransactionFb()
        {
            try
            {
                AbreConnectionFb();
                fbTrans = fbCon.BeginTransaction();
            }
            catch (Exception ex)
            {
                fbTrans = null;
                throw ex;
            }
        }

        //Finaliza as transactions do banco.
        public static void FinalizaTransactionFb()
        {
            try
            {
                fbTrans.CommitRetaining();
            }
            catch (FbException ex)
            {
                FechaConnectionFb();
                throw ex;
            }
            finally
            {
                FechaConnectionFb();
            }
        }

        /// <summary>
        /// Retorna DataSet com resultado de uma consulta SQL
        /// </summary>
        /// <param name="stringSql">comando SQL</param>
        /// <returns>DataSet</returns>
        public static DataSet RetornaDS(string stringSql)
        {
            var resultDS = new DataSet();

            
                try
                {
                    if (fbTrans.Connection.State == ConnectionState.Open)
                    {
                        var fbDA = new FbDataAdapter();
                        var fbCmd = new FbCommand(stringSql, fbCon);
                        fbCmd.Transaction = fbTrans;
                        fbDA.SelectCommand = fbCmd;

                        fbDA.Fill(resultDS);
                    }
                    return resultDS;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            
            
        }

        /// <summary>
        /// Executa comando sql no banco de dados
        /// </summary>
        /// <param name="stringSQL">comando SQL</param>
        /// <param name="fbTr">transaction SQL</param>
        public static void ExecutaSQL(string stringSQL, FbTransaction fbTr)
        {
            try
            {
                if (fbTr.Connection.State == ConnectionState.Open)
                {
                    FbCommand fbCmd = new FbCommand(stringSQL, fbCon);
                    fbCmd.Transaction = fbTr;

                    fbCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                FechaConnectionFb();
                throw ex;
            }
        }

    }
}
